function checkInsuranceIssueDate(objYear, objMonth, objDay, statusMsg) {
    return checkFullDate(objYear, objMonth, objDay, true,  _('Invalid effective date'), statusMsg, true);
}

function checkInsuranceExpireDate(objYear, objMonth, objDay, statusMsg) {
    var res = checkFullDate(objYear, objMonth, objDay, true, _('Empty expiry date'), statusMsg, false);
    if (res == false) {
        return false;
    } 
    var expiredDate =   $.trim(objDay.val()) + "/" + $.trim(objMonth.val()) + "/" + $.trim(objYear.val());
    var result = compareDate(expiredDate, "today");
    if (result == "ltToday") {
        objYear.css("border-color", "red");
        objMonth.css("border-color", "red");
        objDay.css("border-color", "red");
        $('#' + statusMsg).text(_('Expiry date is less than current date')).show();
        return false;
    }
    return true;
}

function checkInsuranceCode_1stPart(obj) {
    var code =   obj.val();
    var availableCodes  =   ['DN', 'HX', 'CH', 'NN',
        'TK', 'HC', 'XK', 'CA', 'HT', 'TB', 'MS', 'XB', 'XN', 'TN', 'CC', 'CK', 'CB', 'KC', 'HD', 'BT', 'HN', 'TC', 'TQ', 'TA',
        'TY', 'TE', 'HG', 'LS', 'CN', 'HS', 'GD', 'TL', 'XV', 'NO'];
    if (!in_array(code, availableCodes, false)) {
        obj.css('border-color', 'red');
        return false;
    }
    obj.css('border-color', '#AAA');
    return true;
}

function checkInsuranceCode_2ndPart(obj) {
    var code =   obj.val();
    if (!code.match(/^[1-7]{1}$/)) {
        obj.css('border-color', 'red');
        return false;
    } 
    obj.css('border-color', '#AAA');
    return true;
}

function checkInsuranceCode_3rdPart(obj) {
    var code =   obj.val();
    if (!code.match(/^[0-9]{2}$/)) {
        obj.css('border-color', 'red');
        return false;
    }
    code    =   parseInt(code, 10);
    if (code <=0 ) {
        obj.css('border-color', 'red');
        return false;
    }
    obj.css('border-color', '#AAA');
    return true;
}

function checkInsuranceCode_4thPart(obj) {
    var code =   obj.val();
    if (!code.match(/^[0-9]{2}$/)) {
        obj.css('border-color', 'red');
        return false;
    }
    code    =   parseInt(code, 10);
    if (code <=0 ) {
        obj.css('border-color', 'red');
        return false;
    }
    obj.css('border-color', '#AAA');
    return true;
}

function checkInsuranceCode_5thPart(obj) {
    var code =   obj.val();
    if (!code.match(/^[0-9]{3}$/)) {
        obj.css('border-color', 'red');
        return false;
    }
    code    =   parseInt(code, 10);
    if (code <=0 ) {
        obj.css('border-color', 'red');
        return false;
    }
    obj.css('border-color', '#AAA');
    return true;
}

function checkInsuranceCode_6thPart(obj) {
    var code =   obj.val();
    if (!code.match(/^[0-9]{5}$/)) {
        obj.css('border-color', 'red');
        return false;
    }
    code    =   parseInt(code, 10);
    if (code <=0 ) {
        obj.css('border-color', 'red');
        return false;
    }
    obj.css('border-color', '#AAA');
    return true;
}

function checkInsuranceCode(obj1, obj2, obj3, obj4, obj5, obj6, msgId) {
    var res1 =   checkInsuranceCode_1stPart(obj1);
    var res2 =   checkInsuranceCode_2ndPart(obj2);
    var res3 =   checkInsuranceCode_3rdPart(obj3);
    var res4 =   checkInsuranceCode_4thPart(obj4);
    var res5 =   checkInsuranceCode_5thPart(obj5);
    var res6 =   checkInsuranceCode_6thPart(obj6);     
    if (res1 && res2 && res3 && res4 && res5 && res6) {
        clearText(msgId);
        return true;
    }
    else {
        showMessage(msgId, _('Invalid insurance code'), 'error');
        return false;
    }
}

function isReadyToSubmitInsurance(msgId) {
    if (!checkInsuranceCode($('#insuranceCode_0'), $('#insuranceCode_1'), $('#insuranceCode_2'), $('#insuranceCode_3'), $('#insuranceCode_4'), $('#insuranceCode_5'), msgId)) {
        return false;
    }
    if (!checkInsuranceIssueDate($('#issued_yyyy'), $('#issued_mm'), $('#issued_dd'), msgId)) {
        return false;
    }
    
    if (!checkInsuranceExpireDate($('#expired_yyyy'), $('#expired_mm'), $('#expired_dd'), msgId)) {
        return false;
    }
    
    var issuedDate   =   $.trim($('#issued_dd').val()) + "/" + $.trim($('#issued_mm').val()) + "/" + $.trim($('#issued_yyyy').val());
    var expiredDate  =   $.trim($('#expired_dd').val()) + "/" + $.trim($('#expired_mm').val()) + "/" + $.trim($('#expired_yyyy').val());
    var res =   compareDate(expiredDate, issuedDate);
    if (res != 'gt') {
        showMessage(msgId, _('Expiry date must be greater than effective date'), 'error');
        return false;
    }
    
    if (!isNotEmpty($('#registeredHospital'), _('Empty insurance registered hospital'), msgId)) {
        return false;
    }
    
    return true;
}

function addInsurance(getInsuraceList, insuranceLbxId, objDialog){
    if (!isReadyToSubmitInsurance('addInsuranceFrm', 'addInsuranceMsg')) {
        return false;
    }
    var data    =   $('#addInsuranceFrm').serialize();
    $.ajax ({   
        type:       "POST", 
        url:        "../Insurance/editInsuranceAjax?set_ajax_view",
        data:       data + "&getInsuranceList=" + getInsuraceList,
        dataType:   "json",
        beforeSend: function() {
            $('#addInsuranceLoaderDiv').show();
        },
        complete:   function() {                        
            $('#addInsuranceLoaderDiv').hide();
        },      
        success:    function(data) {
            if (data.error.code == 'OK') {
            	if (getInsuraceList) {
            	   $('#' + insuranceLbxId).html(data.result.html);
            	   objDialog.dialog('close');
            	}  
            }
            else {
                displayError(data.error.mess);
            }                                                                               
        }
    });
}

function editMedicalInsurance(objDialog, triggeredElement) {
    if (!isReadyToSubmitInsurance('editInsuranceFrm', 'editInsuranceMsg')) {
        return false;
    }
    var data    =   $('#editInsuranceFrm').serialize();
    $.ajax ({   
        type:       "POST", 
        url:        "../Insurance/editInsuranceAjax?set_ajax_view",
        data:       data, 
        dataType:   "json",
        beforeSend: function() {
            $('#editInsuranceLoaderDiv').show();
        },
        complete:   function() {                        
            $('#editInsuranceLoaderDiv').hide();
        },      
        success:    function(data) {
            if (data.error.code == 'OK') {
                objDialog.dialog('close');
                $('#' + triggeredElement).trigger('click');  
            }
            else {
                displayError(data.error.mess);
            }                                                                               
        }
    });
}